﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ProjectTW.Foliage
{
    /// <summary>
    /// This class handles assigning parameters before multi-threaded actions that can be called from outside of unity's main thread.
    /// for example :
    /// position.
    /// </summary>
    public class ThreadItem : MonoBehaviour
    {
        /// <summary>
        /// a 3d position which is being updated from the main thread before important multi-threaded actions.
        /// </summary>
        internal Vector3 m_MainPosition = new Vector3();
        [HideInInspector]
        private Vector2 m_ThreadPositionDepth = new Vector2(-999, -999);

        public Vector3 MainPosition
        {
            get
            {
                return m_MainPosition;
            }
            set
            {
                if (m_MainPosition != value)
                {
                    m_MainPosition = value;

                    OnPositionChanged(value);

                    threadPositionDepth = new Vector2(value.x, value.z);
                }
            }
        }

        public Vector2 threadPositionDepth
        {
            get
            {
                if (m_ThreadPositionDepth == new Vector2(-999, -999))
                {
                    m_ThreadPositionDepth = Vector2.zero;
                }

                return m_ThreadPositionDepth;
            }
            set
            {
                m_ThreadPositionDepth = value;
            }
        }

        protected virtual void OnEnable()
        {
            MainPosition = transform.position;
        }

        protected virtual void OnDisable()
        {
        }

        protected virtual void Update()
        {
            this.MainPosition = transform.position;
        }

        public virtual void UpdateItem()
        {
            MainPosition = this.transform.position;
        }

        protected virtual void OnPositionChanged(Vector3 newPosition)
        {
        }
    }
}
